<?php

namespace app\api\controller;

use app\common\Common;
use app\api\service\UserService;
use think\facade\Request;

class User extends Base
{
    private $service;

    public function __construct(UserService $service)
    {
        $this->service = $service;
    }

    /**
     * 获取关注列表
     */
    public function getConcern()
    {
        $data = $this->service->getConcernList($this->getUserID());
        return Common::success($data);
    }

    // 我的喜欢列表
    public function loveList()
    {
        $params = Request::param();
        $data = $this->service->getLoveList($params, $this->getUserID());
        return Common::success($data);
    }

    /**
     * 获取发布者的详细信息
     * 
     * @dateTime 2022/09/13
     * @author zhixiang0215@126.com
     * @param int admin_id
     * @param int video_id
     * @return json
     */
    public function getUserDetail()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\User::class)->scene('get_user_detail')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getUserDetailInfo($params, $this->getUserID());
        return Common::success($data);
    }

    /**
     * 查看发布者的全部视频
     * 
     * @dateTime 2022/09/13
     * @author zhixiang0215@126.com
     * @param array params
     * @return json
     */
    public function allVideo()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\User::class)->scene('all_video')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getAllVideoData($params, $this->getUserID());
        return Common::success($data);
    }

    /**
     * 粉丝新增或删除
     * 
     * @dateTime 2022/09/06
     * @author zhixiang0215@126.com
     * @param int admin_id
     * @param int type
     * @return json
     */
    public function fansEdit()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\User::class)->scene('fans_edit')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $result = $this->service->fansEdit($params, $this->getUserID());
        if (!$result) {
            return Common::fail();
        }
        return Common::success();
    }

    /**
     * 获取书币数量
     * 
     * @dateTime 2022/09/13
     * @author zhixiang0215@126.com
     * @param int uid
     * @return array
     */
    public function getBookCurrency()
    {
        $params = Request::param();
        $data = $this->service->getUserBookCurrency($this->getUserID(),$params);

        return Common::success($data);
    }

    // 获取用户签到数据
    public function userSignData()
    {
        $params = Request::param();

        $data = $this->service->getUserSignData($params, $this->getUserID());

        return Common::success($data);
    }

    // 用户签到
    public function sign()
    {
        $params = Request::param();

        $data = $this->service->userSign($this->getUserID());

        if (!$data || !empty($data['message'])) {
            $message = !empty($data['message']) ? $data['message'] : Error::FAIL_MESSAGE;
            return Common::fail($message);
        }

        return Common::success($data);
    }

    // 广告签到
    public function adSign()
    {
        $result = $this->service->adSign($this->getUserID());
        if ($result['code'] != 200) {
            return Common::fail($result['message'], $result['code']);
        }
        return Common::success($result['data']);
    }

    // 用户是否关注公众号
    public function isFollowOfficialAccount()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\User::class)->scene('is_follow_official_account')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->userIsFollowOfficialAccount($params, $this->getUserID());

        if (!$data) {
            return Common::fail();
        }
        return Common::success($data);
    }

    // 投诉建议
    public function proposal()
    {
        $params = Request::param();

        try {
            validate(\app\api\validate\User::class)->scene('proposal')->check($params);
        } catch (\Exception $e) {
            return Common::fail($e->getError());
        }

        $data = $this->service->getUserProposal($params, $this->getUserID());

        if (!$data) {
            return Common::fail();
        }
        return Common::success();
    }

    // 添加用户订阅消息
    public function addSubscribe()
    {
        $params = Request::param();

        $bool = $this->service->addUserSubscribeMessage($params, $this->getUserID());

        if (!$bool) {
            return Common::fail();
        }
        return Common::success();
    }

    // 添加用户微信订阅消息
    public function addWechatSubscribe()
    {
        $params = Request::param();
        try {
            validate(\app\api\validate\User::class)->scene('subscribe_config')->check($params);
            $bool = $this->service->addUserWechatSubscribeMessage($params, $this->getUserID());
            if (!$bool) {
                return Common::fail();
            }
            return Common::success();
        } catch (\Exception $e) {
            return Common::fail($e->getMessage());
        }
    }
}